#ifndef _MW_PROCESS_H_INCLUDED_
#define _MW_PROCESS_H_INCLUDED_

#include <sys/types.h>
#include <unistd.h>

#include "event.h"

#define MW_INVALID_PID  -1

typedef void (*mw_spawn_proc_pt)(void *data);

typedef struct {
    pid_t               pid;
    int                 status;
    int                 channel[2];

    mw_spawn_proc_pt   proc;
    void               *data;
    const char               *name;

    unsigned            respawn:1;
    unsigned            just_spawn:1;
    unsigned            detached:1;
    unsigned            exiting:1;
    unsigned            exited:1;

    struct event_base  *base;                                                                                                                                                                                                          
    struct event        notify_event;

} mw_process_t;


typedef struct {
    char         *path;
    char         *name;
    char *const  *argv;
    char *const  *envp;
} mw_exec_ctx_t;

#define mw_value_helper(n)   #n
#define mw_value(n)          mw_value_helper(n)

#define MW_MAX_PROCESSES         1024

#define MW_PROCESS_NORESPAWN     -1
#define MW_PROCESS_JUST_SPAWN    -2
#define MW_PROCESS_RESPAWN       -3
#define MW_PROCESS_JUST_RESPAWN  -4
#define MW_PROCESS_DETACHED      -5


pid_t mw_spawn_process(mw_spawn_proc_pt proc, void *data, const char *name, int respawn);
pid_t mw_execute(mw_exec_ctx_t *ctx);
int mw_init_signals();
void mw_debug_point(void);

extern int            mw_argc;
extern char         **mw_argv;
extern char         **mw_os_argv;

extern pid_t          mw_pid;
extern int            mw_channel;
extern int            mw_process_slot;
extern int            mw_last_process;
extern int            mw_num_coredump;
extern mw_process_t   mw_processes[MW_MAX_PROCESSES];


#endif /* _MW_PROCESS_H_INCLUDED_ */
